﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WaterRevenueMS.Componnent.Entities.ReadMeterMS;
using WaterRevenueMS.Componnent.Common;
using System.ComponentModel.Composition;
using WaterRevenueMS.Core.Repository.ReadMeterMS;
using WaterRevenueMS.Domain.ViewModels.ReadMeterMS.TaskTemplet;
using WaterRevenueMS.Core.Service.Customer;
using WaterRevenueMS.Domain.ViewModels.Customer.MeterInfo;
using WaterRevenueMS.Core.Repository.Customer;

namespace WaterRevenueMS.Core.Service.ReadMeterMS.Impl
{
    [Export(typeof(ITaskTempletService))]
    public class TaskTempletService:CoreServiceBase,ITaskTempletService
    {
        [Import]
        private ITaskTempletRepository TaskTempletRepository { get; set; }
        [Import]
        private IMeterInfoRepository MeterInfoRepository { get; set; }

        public IQueryable<TaskTemplet> TaskTemplets
        {
            get {return TaskTempletRepository.Entities; }
        }

        public OperationResult Insert(TaskTempletModel model)
        {
            var entity = new TaskTemplet
            {
                Ren_WMC = model.Ren_WMC,
                Chao_BND = model.Chao_BND,
                Chao_BYD = model.Chao_BYD,
                Qu_YSZ_No = model.Qu_YSZ_No != 0 ? model.Qu_YSZ_No : null,
                Biao_CSZ_No = model.Biao_CSZ_No != 0 ? model.Biao_CSZ_No : null,
                Chao_BY_No = model.Chao_BY_No != 0 ? model.Chao_BY_No : null,
                ReviseDT = DateTime.Now,
                CreateDT = DateTime.Now,
                IsTaskTemplet = true
            };
            TaskTempletRepository.Insert(entity);
            return new OperationResult(OperationResultType.Success, "添加成功");
        }

        public OperationResult Update(TaskTempletModel model)
        {
            var entity = new TaskTemplet
            {
                Id = model.Id,
                Ren_WMC = model.Ren_WMC,
                Chao_BND = model.Chao_BND,
                Chao_BYD = model.Chao_BYD,
                Qu_YSZ_No = model.Qu_YSZ_No!=0?model.Qu_YSZ_No:null,
                Biao_CSZ_No = model.Biao_CSZ_No!=0?model.Biao_CSZ_No:null,
                Chao_BY_No = model.Chao_BY_No!=0?model.Chao_BY_No:null,
                ReviseDT = DateTime.Now,
                IsTaskTemplet=true
            };
            TaskTempletRepository.Update(entity);
            return new OperationResult(OperationResultType.Success, "跟新成功");
        }

        public OperationResult Delete(int Id)
        {
            var entity = TaskTempletRepository.Entities.Where(t => t.Id == Id);
            if (entity != null)
            {
                TaskTempletRepository.Delete(entity);
            }
            return new OperationResult(OperationResultType.Success, "删除成功"); 
        }


        public OperationResult Insert(TaskGenerateModel model)
        {
            

            var templet = TaskTempletRepository.Entities.FirstOrDefault(t => t.Id == model.TaskTempletId);
            var task = TaskTempletRepository.Entities.FirstOrDefault(t => t.IsTaskTemplet == false && t.Sheng_CZT == 0 && t.Biao_CSZ_No == templet.Chao_BY_No);
            if (task == null)
            {
                var entity = new TaskTemplet
                {
                    Ren_WMC = model.Ren_WMC,
                    Chao_BND = model.Chao_BND,
                    Chao_BYD = model.Chao_BYD,
                    Qu_YSZ_No = templet.Qu_YSZ_No,
                    Biao_CSZ_No = templet.Biao_CSZ_No,
                    Chao_BY_No = templet.Chao_BY_No,
                    ReviseDT = DateTime.Now,
                    CreateDT = DateTime.Now
                };

                var meterInfos = MeterInfoRepository.Entities.Where(t => t.Customer.Biao_CSZ_No == templet.Biao_CSZ_No);

                foreach (var item in meterInfos)
                {
                    //添加任务记录
                    var meterRecord = new MeterRecord
                    {
                        Meter_No = item.Id,
                        Du_SBC = 0,
                        Chao_BDT = DateTime.Now,
                        Yi_BZT = "",
                        Kong_ZZT = "",
                        Tong_XZT = "",
                        Shu_JZT = "",
                        Fa_MZT = "",
                        Dc_Dy_ZT = "",
                        TaskTemplet = entity
                    };
                    entity.MeterRecord.Add(meterRecord);
                    //更新水表信息的任务ID
                }
                TaskTempletRepository.Insert(entity);
                foreach (var item in meterInfos)
                {
                    if (item.RMeter_No == null)
                    {
                        item.RMeter_No = entity.Id;
                        item.Chao_BND = entity.Chao_BND;
                        item.Chao_BYD = entity.Chao_BYD;
                    }
                }
                return new OperationResult(OperationResultType.Success, "添加成功");
            }
            else
            {
                return new OperationResult(OperationResultType.Warning, "该区域的任务已经生成");
            }
            
        }
    }
}
